package hibernate.web;

import hibernate.fileControler.CheckFileHash;
import hibernate.fileControler.ListFiles;
import hibernate.fileControler.MyException;
import hibernate.model.Aygit;
import hibernate.model.Bellekler;
import hibernate.model.Resimler;
import hibernate.model.Sirketprofil;
import hibernate.service.EmmyService;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("UploadPicBean")
@Scope("session")
public class UploadPicBean implements BeanService{

	@Autowired
	private transient EmmyService emmyService;	


	private ArrayList<UploadedFile> fileList = new ArrayList<UploadedFile>();
	private UploadedFile removeFile;

	
	@PostConstruct
	@Override
	public void init() {
	}

	@Override
	public String restart() {
		init();
		return "";
	}

	@Override
	public String deleteView() {
		//emmyService.delete(selectedAygitYzl);
		return "";
	}

	@Override
	public String viewNew() {
		return "";
	}

	@Override
	public String viewDetail() {
		System.out.println("detay detayyy");
		return "";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		Sirketprofil srkprf = (Sirketprofil) emmyService.findById(new Sirketprofil(), (long) 3);
		Aygit aygt = new Aygit(srkprf, "BELLEK", "OCZ", "XRT500SL");
		Set<Resimler> resCollec = new HashSet<Resimler>();
		for(int i=0;i<fileList.size(); i++){
			Resimler res = new Resimler();
			res.setResimKonum(fileList.get(i).getFileName());
			res.setAygit(aygt);
			resCollec.add(res);	
			System.out.println(resCollec.size());
		}
		aygt.setResimlers(resCollec);
		Bellekler blk = new Bellekler(srkprf, aygt, "2.0", "Bellek", "8GB",
				"5mb/s", "3mb/s", "mavi", "45.18mm x 20.28mm x 12.00mm", "Hepsi", "50gr", 
				"var", "var", "yok", "1 Yıl");
		
		emmyService.saveOrUpdate(blk);
		restart();
		return "list";
	}
	
	public void deleteFile(){
		System.out.println("upload delete");
		fileList.remove(removeFile);
	}
	
	public boolean uploadControl() throws Exception{
		String newFileDirectory = "C:/Users/HAVANE/workspace-indigo/yeniEMMY/WebContent/";
		String dest = "productImages/";
		int i = 0;
		try{
			for(i=0; i<fileList.size(); i++){			
				if(! ListFiles.checkFile(newFileDirectory+dest, CheckFileHash.fileCheck(fileList.get(i)))){    
					System.out.println("Dosya Mevcut hatası çıkar");
				}
				//burada dosya adına MD5 ekle öyle kayıt yap
			}
			System.out.println("İşlem tamam");
			SaveOrUpdate();
			return true;
		}catch(MyException mye){
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccesful", fileList.get(i).getFileName() +" : "+ mye.getMessage());  
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return false;
		}
		catch(Exception e){
			System.out.println("uploadControl exp: "+e);
			return false;
		}
	}
	public void handleFileUpload(FileUploadEvent event) {  
	    	System.out.println("upload");
	    	//UploadFiles upsfware = new UploadFiles();
	    	System.out.println("dosya adı : "+event.getFile().getFileName());
	    	fileList.add(event.getFile());	    	
	    	FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesful", event.getFile().getFileName() + " is uploaded.");  
			FacesContext.getCurrentInstance().addMessage(null, msg);
	    	/*try {
	    		if(file != null){
	    			upsfware = upsfware.saveFile(file,"yazilimlar");
	    			if(upsfware.isIslem()){
	    				FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesful", file.getFileName() + " is uploaded.");  
	    				FacesContext.getCurrentInstance().addMessage(null, msg);
	    				//selectedAygitYzl.setDiskKonumu("../yazilimlar/"+upsfware.getFilePath());
	    				SaveOrUpdate();
	    			}
	    			else{
	    				FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccesful", file.getFileName() + " isn't upload.");  
	    				FacesContext.getCurrentInstance().addMessage(null, msg);
					}
				}else{
					FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesful","Completed.");  
    				FacesContext.getCurrentInstance().addMessage(null, msg);
    				SaveOrUpdate();
				}
			}catch(MyException mye){
				FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccesful", file.getFileName() +" : "+ mye.getMessage());  
				FacesContext.getCurrentInstance().addMessage(null, msg);
			}
	    	catch (Exception e) {
				System.out.println("handle Error");
				e.printStackTrace();
			} */
	 } 
	 
	@Override
	public String createQuery() {
		return "";
	}

	@Override
	public void getSession() {
	}

	public ArrayList<UploadedFile> getFileList() {
		return fileList;
	}

	public void setFileList(ArrayList<UploadedFile> fileList) {
		this.fileList = fileList;
	}

	public UploadedFile getRemoveFile() {
		return removeFile;
	}

	public void setRemoveFile(UploadedFile removeFile) {
		this.removeFile = removeFile;
	}
}